// date.cpp
// Implementation of the Date class

#include "date.h"

Date::Date(int day, Month month, int year):   //dates constructor
    _day(day),      //initialise _day with day
    _month(month),  //initialise _month with month
    _year(year)     //initialise _year with year
{
    if (day <= 0)   //check if date is zero or negative
        throw wordNegOrZero();

    if (day > 31)   //check if date is larger than 31
        throw wordTooLarge();


}

Date Date::_default(1, January, 1900 ); //initialize the default date

Date::Date() //default Constructor
{

    _day = _default.day();
    _month = _default.month();
    _year = _default.year();

}

int Date::day() const
{
	return _day;
}

Month Date::month() const
{
	// convert the integer _month to the enumeration type Month
	return static_cast<Month>(_month);
}

int Date::year() const
{
	return _year;
}

bool Date::isLeapYear() const
{
	if ( _year % 4 != 0) return false;
	else {
		if ( _year % 100 != 0 ) return true;
		else
			if ( _year % 400 != 0 ) return false;
			else return true;
	}
}

int Date::daysInMonth() const
{
	switch (_month) {
		case January:
		case March:
		case May:
		case July:
		case August:
		case October:
		case December:
			return 31;
		case April:
		case June:
		case September:
		case November:
			return 30;
		case February:
			if ( isLeapYear() ) return 29;
			else return 28;
		default:
			// should never reach here
			assert(false);
	}
}

// Note, this function is not part of the Date class
void printDate(const Date& date)
{
	cout << date.day() << "/"
		<< date.month() << "/"
		<< date.year()
		<< endl;
}

bool Date::operator==( const Date& rhs ) const
{
    if ( _day == rhs.day() && _month == rhs.month() && _year == rhs.year() )
        return true;
    else
        return false;

}

void Date::increaseDateByOne()
{
    if( daysInMonth() == _day && _month != December )  //to the next month
    {
        _day = 1;
        _month += 1;
    }
    else
     if( daysInMonth() != _day && _month != December )  //to the next day
        _day = _day + 1;


    if( daysInMonth() == _day && _month == December  ) //to the next year
    {
            _day = 1;
            _month = 1;
            _year = _year + 1;
    }

    if( isLeapYear() && _month == February )
        _day = 29;

}

void Date::setDefaultDate( int day, Month month, int year )
{
    _default._day = day;
    _default._month = month;
    _default._year = year;

}

